﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace uREST.Blog.Extensions
{
    public static class XmlExtensions
    {
        public static Resources.Comment ToCommentResource(this XPathNavigator xPathNavigator)
        {
            var xml = new XmlDocument();
            xml.LoadXml(xPathNavigator.InnerXml);

            return new Resources.Comment
            {
                Id = Convert.ToInt32(xml.DocumentElement.SelectSingleNode("id").Value)
            };
        }

        public static List<Resources.Comment> ToCommentResourceList(this XPathNodeIterator xPathNodeIterator)
        {
            var resources = new List<Resources.Comment>();

            var commentNodes = xPathNodeIterator.Current.SelectDescendants(XPathNodeType.Element, false);

            if (commentNodes.Count > 0)
            {
                while (commentNodes.MoveNext())
                    resources.Add(commentNodes.Current.ToCommentResource());
            }

            return resources;
        }
    }
}
